package com.zx.tradeclient.ctrl.index5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.lazylist.ImageLoader;
import com.zx.base.util.URLHelper;
import com.zx.base.widget.LoadMoreListView;
import com.zx.base.widget.LoadMoreListView.OnLoadMoreListener;
import com.zx.base.widget.LoadMoreListViewWithProgress;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index3.LeaveMessageActivity;
import com.zx.tradeclient.entity.MessageInfo;

public class MessagesList {

	private NewsListAdapter newsListAdapter;
	private LoadMoreListViewWithProgress news_view;
	private LoadMoreListView news_listView;
	private ArrayList<MessageInfo> newsList = new ArrayList<MessageInfo>();
	private int page;
	private TradeApp app;
	private ImageLoader imageLoader;
	private Context content;

	public MessagesList(final Context context, View view) {
		content = context;
		page = 1;
		imageLoader = ((TradeApp) context.getApplicationContext()).imageLoader;
		initview();
		app = (TradeApp) context.getApplicationContext();
		news_view = (LoadMoreListViewWithProgress) view.findViewById(R.id.list);
		news_listView = news_view.getListView();
		newsListAdapter = new NewsListAdapter(context, newsList);
		news_listView.setAdapter(newsListAdapter);
		news_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
				MessageInfo mi=newsList.get(position);
				if(mi.getType().equals("2")){
				 Intent intent=new Intent(context,LeaveMessageActivity.class);
				 intent.putExtra("messageID",mi.getId());
				 intent.putExtra("mycompanyID",mi.getRecvId());	
				 intent.putExtra("sendcompanyID",mi.getSendId());
				 intent.putExtra("sendcompanyName",mi.getSendName());
				 if(Constant.debug)Log.d("TAG", mi.getSendId()+"aaaaaaaaaaaaaaaaaaaaaaaaaa");
				 if(Constant.debug)Log.d("TAG", mi.getRecvId()+"aaaaaaaaaaaaaaaaaaaaaaaaaa");
				 if(Constant.debug)Log.d("TAG", mi.getSendName()+"aaaaaaaaaaaaaaaaaaaaaaaaaa");
				 context.startActivity(intent);
				}
			}
		});
		news_listView.setOnLoadMoreListener(new OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				// TODO 拖动刷新
				page++;
				messagesListTask();

			}
		});

		messagesListTask();
	}


	private void initview() {

	}

	private static class ViewHolder {
		public TextView list_message_content,list_message_name;
		public ImageView list_message_icon;
		
	}

	private class NewsListAdapter extends BaseAdapter {

		private List<MessageInfo> topics;
		private LayoutInflater mlinflater;

		public NewsListAdapter(Context context, List<MessageInfo> list) {
			this.mlinflater = LayoutInflater.from(context);
			this.topics = list;
		}

		@Override
		public int getCount() {
			return topics.size();
		}

		@Override
		public Object getItem(int position) {
			return Integer.valueOf(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View view, ViewGroup viewGroup) {
			final MessageInfo topic = topics.get(position);
			ViewHolder vh = new ViewHolder();
			if (topic.getType().equals("1")) {
				view = mlinflater.inflate(R.layout.message_list_item_right, null);
				vh.list_message_icon=(ImageView) view.findViewById(R.id.list_message_icon);
				vh.list_message_content = (TextView) view.findViewById(R.id.list_message_content);
				vh.list_message_name= (TextView) view.findViewById(R.id.list_message_name);
			} else {
				view = mlinflater.inflate(R.layout.message_list_item_left, null);
				vh.list_message_icon=(ImageView) view.findViewById(R.id.list_message_icon);
				vh.list_message_content = (TextView) view.findViewById(R.id.list_message_content);
				vh.list_message_name= (TextView) view.findViewById(R.id.list_message_name);
			}
			
			imageLoader.DisplayImage(URLHelper.mPrefixImg+"/"+topic.getIcon(), vh.list_message_icon, R.drawable.icon_header);
			vh.list_message_name.setText(topic.getSendName());
			vh.list_message_content.setText(Html.fromHtml("发送时间:<br />"+topic.getCreattime()+"<br />内容:	<br />" + topic.getContent()));

			return view;
		}
	}

	private void messagesListTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "messagesList");
		map.put("userId", app.user.getId());
		map.put("currentPage", "" + page);
		map.put("rowCountPerPage", "10");
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					if(Constant.debug)Log.d("TAG", response.getString("dataList"));
				} catch (JSONException e) {
					e.printStackTrace();
				}
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(content, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			String currentPage = response.getString("currentPage");
			String totalPage = response.getString("totalPage");
			if (Constant.debug)
				Log.d("stop", currentPage);
			if (Constant.debug)
				Log.d("stop", totalPage);
			if (currentPage.equals(totalPage)) {
				news_listView.loadComplete(false);
			}

			JSONArray list = new JSONArray(response.getString("dataList"));
			for (int i = 0; i < list.length(); i++) {
				MessageInfo one = new MessageInfo();
				JSONObject message = list.getJSONObject(i);
				one.setType(message.getString("type"));
				one.setId(message.getString("id"));
				one.setRecvName(message.getString("recvName"));
				one.setSendName(message.getString("sendName"));
				one.setIcon(message.getString("sendIcon"));
				one.setRecvId(message.getString("recvId"));
				one.setSendId(message.getString("sendId"));
				one.setTitle(message.getString("title"));
				one.setContent(message.getString("content"));
				one.setCreattime(message.getString("sendTime"));
				newsList.add(one);
			}
			newsListAdapter.notifyDataSetChanged();
			news_view.show();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
